﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SIMS.Global;
using SIMS.Database;
using System.Collections;
using System.Data;

namespace SIMS.School
{
    public class CourseStudent
    {
        public int Id { get; set; }
        public int CompanyId { get; set; }
        public string CompanyName { get; set; }

        public int CourseId { get; set; }
        public string CourseName { get; set; }

        public int CourseDetailId { get; set; }
        public string CourseDetailName { get; set; }

        public int StudentProfileId { get; set; }
        public string StudentName { get; set; }

        public string Remark { get; set; }

        public bool IsActive { get; set; }
        public DateTime CreatedDate { get; set; }
        public DateTime ModifiedDate { get; set; }

        public MyError Error { get; set; }
    }

    public class CourseStudentList : List<CourseStudent>
    {
        public MyError Error { get; set; }
        public CourseStudentList()
        {
        }
    }

    public class CourseStudentDB
    {
        MyDatabase _db = new MyDatabase();

        public CourseStudent Save(CourseStudent CourseStudent)
        {
            try
            {
                SortedList _param = new SortedList();
                _param.Add("@Id", CourseStudent.Id);
                _param.Add("@CourseDetailID", CourseStudent.CourseDetailId);
                _param.Add("@StudentProfileId", CourseStudent.StudentProfileId);
                if (CourseStudent.Remark != string.Empty)
                {
                    _param.Add("@Remark", CourseStudent.Remark);
                }
                else
                {
                    _param.Add("@Remark", DBNull.Value);
                }

                _param.Add("@IsActive", CourseStudent.IsActive);

                int _courseStudentId = _db.ExecuteSP("usp_CourseStudentInsertUpdateSingleItem", _param);

                if (_courseStudentId > 0)
                {
                    CourseStudent.Error.ErrorNo = 0;
                }
                else
                {
                    if (_courseStudentId == -100)
                    {
                        CourseStudent.Error.ErrorNo = _courseStudentId;
                        CourseStudent.Error.ErrorMessage = "Student had already enrolled this course.";
                    }
                    else
                    {
                        CourseStudent.Error.ErrorNo = 1;
                        CourseStudent.Error.ErrorMessage = "No row affect when saving school's CourseStudent.";
                    }
                }
            }
            catch (Exception Ex)
            {
                CourseStudent.Error = new MyError();
                CourseStudent.Error.ErrorNo = 10002;
                CourseStudent.Error.ErrorLocation = "CourseStudent";
                CourseStudent.Error.ErrorMessage = Ex.Message;
                CourseStudent.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CourseStudent");
            }

            return CourseStudent;
        }

        public CourseStudentList GetCourseStudentList(int CompanyId)
        {
            CourseStudentList retValue = new CourseStudentList();

            try
            {
                DataTable _dt = new DataTable("tblCourseStudent");

                SortedList _param = new SortedList();
                _param.Add("@CompanyId", CompanyId);
                _dt = _db.GetDataTableBySP("usp_CourseStudentSelectItemList", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    foreach (DataRow _dr in _dt.Rows)
                    {
                        CourseStudent _CourseStudent = new CourseStudent();
                        FillData(_dr, _CourseStudent);
                        retValue.Add(_CourseStudent);
                    }
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "CourseStudent";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CourseStudent");
            }

            return retValue;
        }

        public CourseStudent GetCourseStudent(int Id)
        {
            CourseStudent retValue = new CourseStudent();

            try
            {

                DataTable _dt = new DataTable("tblCourseStudent");

                SortedList _param = new SortedList();
                _param.Add("@Id", Id);
                _dt = _db.GetDataTableBySP("usp_CourseStudentSelectSingleItem", _param);

                if (_dt != null && _dt.Rows.Count > 0)
                {
                    FillData(_dt.Rows[0], retValue);
                }
            }
            catch (Exception Ex)
            {
                retValue.Error.ErrorNo = 10001;
                retValue.Error.ErrorLocation = "CourseStudent";
                retValue.Error.ErrorMessage = Ex.Message;
                retValue.Error.ErrorStack = Ex.StackTrace;

                MyError.CreateExceptionLog(Ex, "CourseStudent");
            }

            return retValue;
        }

        private void FillData(DataRow aRow, CourseStudent CourseStudent)
        {
            CourseStudent.Error = new MyError();
            CourseStudent.Id = Convert.ToInt32(aRow["Id"].ToString());
            CourseStudent.CompanyId = Convert.ToInt32(aRow["CompanyId"].ToString());
            CourseStudent.CourseId = Convert.ToInt32(aRow["CourseId"].ToString());
            CourseStudent.CourseDetailId = Convert.ToInt32(aRow["CourseDetailId"].ToString());
            CourseStudent.StudentProfileId = Convert.ToInt32(aRow["StudentProfileId"].ToString());

            if (aRow["Remark"] != DBNull.Value)
            {
                CourseStudent.Remark = aRow["Remark"].ToString();
            }
            else
            {
                CourseStudent.Remark = string.Empty;
            }

            CourseStudent.CompanyName = aRow["CompanyName"].ToString();
            CourseStudent.CourseName = aRow["CourseName"].ToString();
            CourseStudent.CourseDetailName = aRow["CourseDetailName"].ToString();
            CourseStudent.CourseDetailName = aRow["StudentName"].ToString();

            CourseStudent.CreatedDate = Convert.ToDateTime(aRow["CreatedDate"].ToString());
            CourseStudent.ModifiedDate = Convert.ToDateTime(aRow["ModifiedDate"].ToString());

            CourseStudent.IsActive = Convert.ToBoolean(aRow["IsActive"].ToString());

            CourseStudent.Error.ErrorNo = 0;
        }
    }
}
